package cn.chinaunicom.sdsi.gacx.quartz.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.chinaunicom.sdsi.frm.base.BaseService;
import cn.chinaunicom.sdsi.frm.sys.R;
import cn.chinaunicom.sdsi.frm.sys.entity.SysStaff;
import cn.chinaunicom.sdsi.gacx.author.dao.mybatis.AuthorMapper;
import cn.chinaunicom.sdsi.gacx.author.service.AuthorService;
import cn.chinaunicom.sdsi.gacx.common.EncoderUtils;
import cn.chinaunicom.sdsi.gacx.quartz.dao.mybatis.GaTaskConfigMapper;
import cn.chinaunicom.sdsi.gacx.quartz.dao.mybatis.GaTaskMapper;
import cn.chinaunicom.sdsi.gacx.quartz.service.QuartzService;
import cn.chinaunicom.sdsi.gacx.quartz.entity.GaTask;
import cn.chinaunicom.sdsi.gacx.quartz.entity.GaTaskConfig;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author chenxl 20170729
 *
 */
@Service ("quartzService")
public class QuartzServiceImpl extends BaseService implements QuartzService {

	@Autowired
	private GaTaskConfigMapper gaTaskConfigMapper;
	@Autowired
	private GaTaskMapper gaTaskMapper;
	

	/**
	 * 获取任务数据
	 */
	@Override
	public JSONObject selectAllTaskByType(String type) {
		List<Map> dataList = new ArrayList<>();

		List<GaTaskConfig> list = gaTaskConfigMapper.selectAllTaskByType(type);
		for (GaTaskConfig gc : list) {
			Map<String, Object> dataMap = new LinkedHashMap<>();
			dataMap.put("jobId", gc.getJobId());
			dataMap.put("jobName", gc.getJobName());
			dataMap.put("doFrequency", gc.getDoFrequency());
			dataMap.put("isModify", gc.getIsModify());
			dataMap.put("doClass", gc.getDoClass());
			dataList.add(dataMap);
		}

		JSONObject rObj = new JSONObject();
		rObj.element("data", dataList);
		return rObj;
	}
	
	
	/**
	 * 根据任务编号  更新定时任务频率为已更改状态
	 */
	@Override
	public void updateJobIsModify(String jobId) {
		gaTaskConfigMapper.updateJobIsModify(Integer.parseInt(jobId));
	}
	
	/**
	 * 根据JOBID获取有效时间内的监控预警配置信息
	 */
	@Override
	public JSONObject getJobTaskByJobId(String jobId) {
		List<Map> dataList = new ArrayList<>();
		System.out.println("print getJobTaskByJobId===="+jobId);
		List<GaTask> list = gaTaskMapper.selectByJobId(Integer.parseInt(jobId));
		for (GaTask gt : list) {
			Map<String, Object> dataMap = new LinkedHashMap<>();
			dataMap.put("taskId", gt.getTaskId());
			dataMap.put("areaId", gt.getAreaId());
			dataMap.put("populationId", gt.getPopulationId());
			dataMap.put("specifiedOutCity", gt.getSpecifiedOutCity());
			dataMap.put("specifiedAreaId", gt.getSpecifiedAreaId());
			dataMap.put("value", gt.getValue());
			dataMap.put("startDate", gt.getStartDate());
			dataMap.put("endDate", gt.getEndDate());
			dataMap.put("jobId", gt.getJobId());
			dataMap.put("attribOne", gt.getAttribOne());
			dataMap.put("attribTwo", gt.getAttribTwo());
			dataMap.put("attribThree", gt.getAttribThree());
			dataList.add(dataMap);
		}

		JSONObject rObj = new JSONObject();
		rObj.element("data", dataList);
		return rObj;
	}

}